package com.szgr.baoyin.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.szgr.baoyin.R;
import com.szgr.baoyin.model.MyProtectItem;
import com.szgr.baoyin.myview.CircleImageView;

import java.util.List;

/**
 * Created by Jimbo on 2015/10/10.
 */
public class MyProtectListAdapter extends BaseAdapter {
    private Context mContext;
    private List<MyProtectItem> data;
    private View.OnClickListener listener;
    public MyProtectListAdapter(Context context, List<MyProtectItem> data,View.OnClickListener listener){
        mContext = context;
        this.data = data;
        this.listener = listener;
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_my_protect_list_view,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();

        MyProtectItem myProtectItem = data.get(position);
        holder.tv_id.setText(myProtectItem.getSerialno());
        holder.tv_name.setText(myProtectItem.getBrand());
        String end_date = myProtectItem.getEnd_date();
        String[] split = end_date.split(" ");
        holder.tv_time.setText(split[0]);
        holder.tv_apply.setTag(position);
        if(myProtectItem.isHasBuy()){
            holder.tv_apply.setText("查看详情");
            holder.tv_apply.setBackgroundResource(R.drawable.bg_common_button);
        }else{
            holder.tv_apply.setText("立即申请");
            holder.tv_apply.setBackgroundResource(R.drawable.bg_yellow_shape);
        }
        holder.tv_apply.setOnClickListener(listener);

        return convertView;
    }

    public void setData(List<MyProtectItem> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    private class ViewHolder {
        TextView tv_id;
        TextView tv_name;
        TextView tv_time;
        TextView tv_apply;

        public ViewHolder(View view){
            tv_id = (TextView) view.findViewById(R.id.protect_id);
            tv_name = (TextView) view.findViewById(R.id.protect_name);
            tv_time = (TextView) view.findViewById(R.id.protect_time);
            tv_apply = (TextView) view.findViewById(R.id.protect_apply);
        }
    }
}
